package com.thtfpc.stroke.screening.service;

import com.thtfpc.stroke.screening.bean.*;
import com.thtfpc.stroke.screening.dao.*;
import com.thtfpc.stroke.screening.model.*;
import com.thtfpc.stroke.screening.util.CodeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class MedScreeningServiceImp implements  MedScreeningService {

    @Autowired
    private MedScreeningInfoMapper medScreeningInfoMapper ;

    @Autowired
    private MedScreeningPatientMapper  medScreeningPatientMapper ;

    @Autowired
    private MedScreeningAuxiliaryMapper medScreeningAuxiliaryMapper ;

    @Autowired
    private MedScreeningPrognosisMapper medScreeningPrognosisMapper;

    @Autowired
    private MedScreeningTreatmentMapper medScreeningTreatmentMapper ;

    @Autowired
    private MedScreeningClinicalMapper medScreeningClinicalMapper;



    @Override
    public MedScreeningInfo recordBaseInfo(MedScreeningInfoReq req) {

        String recoderId = CodeUtil.generationCodeRecordId();
        String patientId = CodeUtil.generationCodePatientId();

        MedScreeningInfo data =new MedScreeningInfo();
        data.setRecordId(recoderId);
        data.setPatientId(patientId);
        BeanUtils.copyProperties(req,data);
        data.setCreateDate(new Date());
        medScreeningInfoMapper.insert(data);
        return data;
    }

    @Override
    public MedScreeningClinical recordClinic(MedScreeningClinicalReq req) {
        MedScreeningClinical query =new MedScreeningClinical() ;
        query.setRecordId(req.getRecordId());
        MedScreeningClinical dataDb= medScreeningClinicalMapper.selectOne(query);
        MedScreeningClinical data =new MedScreeningClinical() ;
        BeanUtils.copyProperties(req,data);
        if(dataDb!=null){
            data.setId(dataDb.getId());
            data.setUpdateTime(new Date());
            medScreeningClinicalMapper.updateByPrimaryKeySelective(data);
        }else{
            data.setCreateTime(new Date());
            medScreeningClinicalMapper.insertSelective(data);

        }
        return data;
    }

    @Override
    public MedScreeningAuxiliary recordAuxiliary(MedScreeningAuxiliaryReq req) {
        MedScreeningAuxiliary query =new MedScreeningAuxiliary() ;
        query.setRecordId(req.getRecordId());
        MedScreeningAuxiliary dataDb= medScreeningAuxiliaryMapper.selectOne(query);
        MedScreeningAuxiliary data =new MedScreeningAuxiliary() ;
        BeanUtils.copyProperties(req,data);
        if(dataDb!=null){
            data.setId(dataDb.getId());
            data.setUpdateDate(new Date());
            medScreeningAuxiliaryMapper.updateByPrimaryKeySelective(data);
        }else{
            data.setCreateDate(new Date());
            medScreeningAuxiliaryMapper.insertSelective(data);

        }
        return data;
    }

    @Override
    public MedScreeningTreatment recordTreatment(MedScreeningTreatmentReq req) {
        MedScreeningTreatment query =new MedScreeningTreatment() ;
        query.setRecordId(req.getRecordId());
        MedScreeningTreatment dataDb= medScreeningTreatmentMapper.selectOne(query);
        MedScreeningTreatment data =new MedScreeningTreatment() ;
        BeanUtils.copyProperties(req,data);
        if(dataDb!=null){
            data.setId(dataDb.getId());
            data.setUpdateTime(new Date());
            medScreeningTreatmentMapper.updateByPrimaryKeySelective(data);
        }else{
            data.setCreateTime(new Date());
            medScreeningTreatmentMapper.insertSelective(data);

        }
        return data;
    }

    @Override
    public MedScreeningPrognosis recordPrognosis(MedScreeningPrognosisReq req) {
        MedScreeningPrognosis query =new MedScreeningPrognosis() ;
        query.setRecordId(req.getRecordId());
        MedScreeningPrognosis dataDb= medScreeningPrognosisMapper.selectOne(query);
        MedScreeningPrognosis data =new MedScreeningPrognosis() ;
        BeanUtils.copyProperties(req,data);
        if(dataDb!=null){
            data.setId(dataDb.getId());
            data.setUpdateTime(new Date());
            medScreeningPrognosisMapper.updateByPrimaryKeySelective(data);
        }else{
            data.setCreateTime(new Date());
            medScreeningPrognosisMapper.insertSelective(data);

        }
        return data;
    }

}
